package com.csgc.camera.widget.mask

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.get
import com.csgc.camera.constants.MaskConfig
import com.csgc.camera.events.ChangeRemarkStatus
import com.csgc.camera.events.MaskContentStateEvent
import com.csgc.camera.events.MaskDataStyleEvent
import com.csgc.camera.events.MaskEditDateEvent
import com.csgc.camera.events.MaskLocationEvent
import com.csgc.camera.events.MaskUpdateContent
import com.csgc.camera.events.TimeChangeEvent
import com.csgc.camera.util.TimeUtil
import com.csgc.camera.widget.dialog.CustomMaskHandle
import com.drake.channel.receiveEventHandler
import com.hlyj.http.base.tool.lib_hlyj_base.net.ApiCallback
import com.hlyj.http.base.tool.lib_hlyj_base.weather.WeatherResultBean
import com.ok.common.api.GlobalApi
import kotlinx.coroutines.Job

open class MaskBase:LinearLayout, CustomMaskHandle {

    protected var location: TextView?=null
    protected var remark: TextView?=null
    protected var remarkLayout: View?=null

    protected var date: TextView?=null
    protected var week: TextView?=null
    protected var time: TextView?=null
    protected var customMaskContainer:LinearLayout?=null
    protected var dateStyle:String = ""

    private var receiver: Job?=null
    private var receiverChangeDateTime: Job?=null
    private var receiverChangeDateStyle: Job?=null
    private var receiverChangeDate: Job?=null
    private var receiverUpdateContent: Job?=null
    private var receiverLocation: Job?=null

    private var receiverContentStatus: Job?=null
    var remarkNeedTitle = true

    var customTextColor = Color.WHITE

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    var weather: String? = null
    // 添加获取天气的方法
    private fun fetchWeather() {
        // 这里需要传入城市名，可以从位置信息中获取
        val city = MaskConfig.maskLocation // 假设有一个获取位置的方法
        if (city.isNotEmpty()) {
            GlobalApi.get24HoursWeather(city, object : ApiCallback<WeatherResultBean?> {
                override fun onSuccess(model: WeatherResultBean?) {
                    // 更新天气显示
                    Log.e("TAG","天气数据：${model}")
                }

                override fun onFailure(msg: String) {

                }

                override fun onFinish() {
                    // 可以添加完成后的处理
                }
            })
        }
    }

    init {
        receiver = receiveEventHandler<ChangeRemarkStatus> {
            remark?.visibility = if (it.isShow) VISIBLE else GONE
            remarkLayout?.visibility = if (it.isShow) VISIBLE else GONE
        }

        //时间自动变化
        receiverChangeDateTime = receiveEventHandler<TimeChangeEvent> {
            changeDateTime()
        }
        //时间格式变化
        receiverChangeDateStyle = receiveEventHandler<MaskDataStyleEvent> {
            dateStyle = it.pattern
            changeDateTime()
        }

        //时间编辑修改
        receiverChangeDate = receiveEventHandler<MaskEditDateEvent> {
            changeDateTime()
        }
        //内容更新
        receiverUpdateContent =  receiveEventHandler<MaskUpdateContent> {
            if(it.title == "备注"){
                if(remarkNeedTitle){
                    remark?.text = "${it.title}:${it.content}"
                }else{
                    remark?.text = "${it.content}"
                }

            }else{
                updateItemContent(it.title,it.content)
            }

        }
        //位置
        receiverLocation = receiveEventHandler<MaskLocationEvent> {
            if(it.location.isNotEmpty()){
                location?.text = it.location
            }

        }

        //显示状态
        receiverContentStatus = receiveEventHandler<MaskContentStateEvent> {
            if (it.title == "地点"){
                location?.visibility = if (it.isShow) VISIBLE else GONE
            }
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        receiver?.cancel()
        receiverChangeDateTime?.cancel()
        receiverChangeDateStyle?.cancel()
        receiverChangeDate?.cancel()
        receiverUpdateContent?.cancel()
        receiverLocation?.cancel()
        receiverContentStatus?.cancel()
    }

    override fun onAddCustom() {
        val text = TextView(context)
        text.setTextColor(customTextColor)
        text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14f)
        val lastData = MaskConfig.customMaskInfo[MaskConfig.customMaskInfo.size-1]
        text.text = "${lastData.title}:${lastData.content}"
        customMaskContainer?.addView(text)


    }

    override fun changeCustomStatus() {
        val count = customMaskContainer?.childCount?:0
        for (i in 0 until count ){
            val isOpen = MaskConfig.customMaskInfo[i].isOpen
            customMaskContainer?.get(i)?.visibility = if (isOpen) View.VISIBLE else View.GONE
        }
    }

    override fun changeDateTime() {
        var currentTime = System.currentTimeMillis()
        if (MaskConfig.maskEditDate > 0){
            currentTime = MaskConfig.maskEditDate
        }

        date?.text = TimeUtil.getTimeFormat(currentTime,dateStyle)
        week?.text = TimeUtil.dateOfWeek(currentTime)


        time?.text = TimeUtil.dateFormatHM(currentTime)
    }

    open fun updateItemContent( title:String, content:String){

    }
}